import React from 'react';
import { toJS } from 'mobx'
import { inject, observer } from 'mobx-react';
import { Route, withRouter, Switch, Redirect } from 'react-router-dom';
import { Icon, SearchBar, Tools, Popover,Toast,Accordion } from 'weaver-mobile';
import { TabPage, SearchAdvanced } from 'weaver-mobile-page';
import WeaUtils from '../../../public/wea-utils';
import BlogComment from '../../public/BlogComment';
import HisBlogRoute from '../../hisBlog/HisBlogRoute';

const getLabel = Tools.getLabel;

const store = [
  'weiBoStore'

]

@withRouter
@inject(...store)
@observer
export default class ShareToMeWeibo extends React.Component {
  static defaultProps = {
    prefixCls: 'blog-myblog'
  }

  constructor(props) {
    super(props);
    this.state = {
      id: '',
      visible: false
    }
  }

  componentDidMount() {
    const { weiBoStore: { search, getMenuCountData } } = this.props;
    getMenuCountData();
    search.getData({
      url: `/api/blog/base/blogSearchCondition`,
      method: `GET`,
      params: { listType: 'isShareToMeAll' }
    });

  }

  componentWillReceiveProps(nextProps) {
    // fix： 手动切路由的时候，不走接口问题
    const prePath = this.props.location.pathname;
    const curPath = nextProps.location.pathname;
    if (curPath && curPath !== prePath) {
      if (curPath !== '/userCenter/shareToMe/shareToMeWeibo/myBlogAdsearch' && prePath !== '/userCenter/shareToMe/shareToMeWeibo/myBlogAdsearch') {//高级搜索不初始化
        nextProps.weiBoStore.initCondition();
      }

    }
  }

  componentWillUnmount() {
    const { weiBoStore: { initCondition } } = this.props;
    initCondition();
  }

  // 点击tab页切换
  changeTab = (tab, index) => {
    const { weiBoStore: { initCondition, setCommentCount } } = this.props;
    let path = '';
    initCondition()
    switch (index) {
      case 0: path = '/userCenter/shareToMe/shareToMeWeibo'; break;
      case 1: path = '/myBlog/comment'; setCommentCount(); break;
      case 2: path = '/myBlog/prise'; break;
      case 3: path = '/myBlog/atMe'; break;
      default: return;
    }
    this.props.history.push(path);
  }

  // 更具路由获得当前的page
  getPage = (path) => {
    let page = 0;
    switch (path) {
      case '/userCenter/shareToMe/shareToMeWeibo': page = 0; break;
      case '/myBlog/comment': page = 1; break;
      case '/myBlog/prise': page = 2; break;
      case '/myBlog/atMe': page = 3; break;
      default: return;
    }
    return page;
  }

  onSearch = (val) => {
    const { weiBoStore: { getMyBlogList } } = this.props;
    getMyBlogList({ content: val })
  }

  // 微博
  renderBlog = () => {

  }

  onSelect = (opt, index) => {
    this.props.history.push({ pathname: '/userCenter/shareToMe/shareToMe' });
  }

  sendRemindUnsubmitByRemindIds = (replyList) => {
    const datas = toJS(replyList);
    let userIds = "";
    let workdates = "";
    datas.forEach((item, key) => {
      if (item.content == '') {
        userIds+=item.userid + ",";
        workdates+=item.workdate + ",";
      }
    });
    if(userIds==""){
      return;
    }
    let params = {userIds:userIds,workdates:workdates}
    const options = {
      url: '/api/blog/base/sendRemindUnsubmitByRemindIds',
      method: 'POST',
      params: params
    };
    Tools.callApi(options).then((src) => {
      if (src.status == "1") {
        Toast.info(getLabel('391235','已全部提醒') , 1);
      }
    });
  }


  render() {
    const { weiBoStore: { search, searchBlog, replyList, updateAdSearchSubject, searchAdValue, setStoreState, photoSwipePage, thisDate, noSub }, prefixCls } = this.props;
    const btnData = [
      {
        key: 'reset',
        text: getLabel(2022, "重置"),
        onClick: () => {
          setStoreState({ searchAdValue: '' });
          search.form.resetFields();
        }
      },
      {
        key: 'submit',
        text: getLabel(826, "确定"),
        onClick: () => {
          searchBlog();
          this.props.history.push("/userCenter/shareToMe/shareToMeWeibo");
        },
        type: 'primary',
      }
    ];
    let _dateF1 = WeaUtils.DateFormat(thisDate, 'M月dd日').replace('月', getLabel(33452, '月')).replace('日', getLabel(390, '日'));
    let _weekF1 = WeaUtils.DateFormat(thisDate, '周E').replace('周日', getLabel(398, '星期日')).replace('周一', getLabel(392, '星期一')).replace('周二', getLabel(393, '星期二'))
      .replace('周三', getLabel(394, '星期三')).replace('周四', getLabel(395, '星期四')).replace('周五', getLabel(396, '星期五')).replace('周六', getLabel(397, '星期六'));

    return (
      <div className='blog-center-myFans'>
        <div style={{ backgroundColor: '#717171', color: '#FFFFFF', height: '25px', display: 'flex', justifyContent: 'center', alignItems: 'center' }}>{getLabel(513653, '分享给我')}{getLabel(386128, '微博')}{getLabel(27596, '总览')}</div>
     
        <SearchBar
          value={searchAdValue}
          placeholder={getLabel(500351, '请输入关键字')}
          showExtraButton
          extraButton={[{ key: 'advanced', show: <Icon key='filter-o' onClick={() => this.props.history.push(search.path)} type='filter-o' /> }
            , {
            key: 'drop',
            show: (
              <Popover
                mask
                visible={this.state.visible}
                overlay={[
                  <Popover.Item>{getLabel(513653, '分享给我')}</Popover.Item>
                ]
                }
                onSelect={(v, index) => this.onSelect(v, index)}
              >
                <Icon key='ellipsis' type='ellipsis' />
              </Popover>
            ),
          },
          ]}
          onChange={val => updateAdSearchSubject(val)}
          onSubmit={searchBlog}
        />
        <SearchAdvanced
          store={search}
          onChange={(condition, value) => {
            if (condition.domkey[0] === 'content') {
              setStoreState({ searchAdValue: value });
            }
          }}
          buttonGroupData={btnData}
        />
        <div className={`${prefixCls}-blog-main`} style={{ top: "66px" }}>
          <div style={{ backgroundColor: "#EFEFF4", height: "30px" }}>
            <div style={{ paddingTop: "8px", paddingLeft: "5px", color: "#999999" }}>{_dateF1}{_weekF1}&nbsp;&nbsp;<span style={{ color: 'red' }}>{noSub + getLabel(500497, '人') + getLabel(32555, '未提交')}</span>
              <span onClick={(e) => this.sendRemindUnsubmitByRemindIds(replyList)} style={{ paddingLeft: '115px' }}><Icon type="blog-fill" size="xxs" /></span>
            </div>
          </div>
          <BlogComment path="/userCenter/shareToMe/shareToMeWeibo" id="writeMyBlog" replyStore={this.props.weiBoStore} storeName='myBlog' photoSwipePage={photoSwipePage} />
        </div>
      </div>
    );
  }
}